﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;


namespace UnionMall.Web.File.Controllers
{
    public class UploadController : Controller
    {
        //private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _HostingEnvironment;
        //public UploadController(
        //   IHostingEnvironment HostingEnvironment
        //    )
        //{
        //    _HostingEnvironment = HostingEnvironment;
        //}
        [HttpGet]
        public ActionResult FileUpLoad()
        {
            return View();
        }
        [HttpPost]
        public ActionResult FileUpLoadAjax()
        {
            //var rtnMsg = new ResultMsg { Success = false };
            if (Request.Form.Files.Count <= 0)
                return Json(new { });
            var uploadfile = Request.Form.Files[0];
            string rtnPath = string.Format("/Upload/Images/{0}/", DateTime.Now.ToString("yyyy/MM/dd"));
            string directoryPath = "";//_HostingEnvironment.WebRootPath + rtnPath;
            string fileName = Guid.NewGuid().ToString("N") + ".png";
            var savePath = directoryPath + fileName;
            if (!Directory.Exists(directoryPath))
                Directory.CreateDirectory(directoryPath);
            using (FileStream fs = System.IO.File.Create(savePath))
            {
                uploadfile.CopyTo(fs);
                fs.Flush();
            }
          //  rtnMsg = new ResultMsg { Success = true, Data = rtnPath + fileName };
            return Json(new { });
        }

        [HttpPost]
        public ActionResult FileUpStream([FromBody]string objData)
        {
           // var rtnMsg = new ResultMsg { Success = false, Info = "上传失败" };
            if (!ModelState.IsValid)
                return Json(new { });
            string rtnPath = string.Format("/Upload/{0}/Images/{1}/", "tenantId", DateTime.Now.ToString("yyyy/MM/dd"));
            string directoryPath = "";// _HostingEnvironment.WebRootPath + rtnPath;
            string fileName = Guid.NewGuid().ToString("N") + ".png";
            var savePath = directoryPath + fileName;
            if (!Directory.Exists(directoryPath))
                Directory.CreateDirectory(directoryPath);
            try
            {
              //  Utils.SaveFile(objData.file, savePath);
                //rtnMsg = new ResultMsg { Success = true, Data = rtnPath + fileName, Info = "上传成功" };
            }
            catch (Exception err)
            {
               // rtnMsg = new ResultMsg { Success = false, Info = "上传失败," + err.Message };
            }
            return Json(new { });
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="moduleStr">当前模块 [product,ordering....]</param>
        /// <param name="materialCode"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("upload")]
        public async Task<string> UpFile(IFormFile file = null)
        {

            if (Request.Form.Files.Count == 0)
                return ("未检测到上传文件！");

            file = Request.Form.Files["file"];
            if (file != null)
            {

                return ("上传成功！");
            }
            else
            {
                return ("未检测到上传文件！");
            }
        }
    }
}